package band.wukong.practice.util.mess;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Desc: 文件工具类
 * Author: sunjs
 * Date: 2009-11-5 11:09:08
 */
public final class FileUtils {

    /**
     * 拷贝文件。如果charset为null，则采用本地平台的字符编码
     *
     * @param res        源文件
     * @param resCharset 源文件字符集
     * @param target        目标文件
     * @param targetCharset 目标文件字符集
     */
    public static void copyFile(String res, String resCharset, String target, String targetCharset) {
        InputStream in = null;
        try {
            in = new FileInputStream(res);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        InputStreamReader reader = null;
        if (null == resCharset) {
            reader = new InputStreamReader(in);
        } else {
            try {
                reader = new InputStreamReader(in, resCharset);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        BufferedReader br = new BufferedReader(reader);

        OutputStream out = null;
        try {
            out = new FileOutputStream(target);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        OutputStreamWriter writer = null;
        if (null == targetCharset) {
            writer = new OutputStreamWriter(out);
        } else {
            try {
                writer = new OutputStreamWriter(out, targetCharset);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        BufferedWriter bw = new BufferedWriter(writer);
        PrintWriter pw = new PrintWriter(bw, true);


        String data;
        try {
            while (null != (data = br.readLine())) {
                pw.println(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        /*
            异常在方法前面抛出的版本，无catch异常，更加易读：
            InputStream in = new FileInputStream(res);
            InputStreamReader reader = null;
            if (null == resCharset) {
                reader = new InputStreamReader(in);
            } else {
                reader = new InputStreamReader(in, resCharset);
            }
            BufferedReader br = new BufferedReader(reader);

            OutputStream out = new FileOutputStream(des);
            OutputStreamWriter writer = null;
            if (null == desCharset) {
                writer = new OutputStreamWriter(out);
            } else {
                writer = new OutputStreamWriter(out, desCharset);
            }
            BufferedWriter bw = new BufferedWriter(writer);
            PrintWriter pw = new PrintWriter(bw, true);


            String data;
            while (null != (data = br.readLine())) {
                pw.println(data);
            }
            br.close();
            bw.close();
         */
    }

    /**
     * 复制远程文件到本机
     *
     * @param remoteFile 远程文件地址
     * @param localFile  复制到本机地址
     * @throws RuntimeException
     */
    public static void copyRemoteFileAsLocal(String remoteFile, String localFile) {
        DataInputStream in = null;
        DataOutputStream out = null;
        try {
            URL url = new URL(remoteFile);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            in = new DataInputStream(connection.getInputStream());
            out = new DataOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[4096];
            int count = 0;
            while ((count = in.read(buffer)) > 0) {
                out.write(buffer, 0, count);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (null != out) {
                    out.flush();
                    out.close();
                }
                if (null != in) in.close();
            } catch (IOException ignored) {
            }
        }
    }

}
